<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - coverage.lcov - src/utility/mqtt_client_byte_buffer.dart</title>
  <link rel="stylesheet" type="text/css" href="../../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../../index.html">top level</a> - <a href="index.html">src/utility</a> - mqtt_client_byte_buffer.dart<span style="font-size: 80%;"> (source / <a href="mqtt_client_byte_buffer.dart.func.html">functions</a>)</span></td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">coverage.lcov</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">50</td>
            <td class="headerCovTableEntry">50</td>
            <td class="headerCovTableEntryHi">100.0 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2017-10-09</td>
            <td></td>
            <td class="headerItem">Functions:</td>
            <td class="headerCovTableEntry">0</td>
            <td class="headerCovTableEntry">0</td>
            <td class="headerCovTableEntryHi">-</td>
          </tr>
          <tr><td><img src="../../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<a name="1"><span class="lineNum">       1 </span>            : /*</a>
<span class="lineNum">       2 </span>            :  * Package : mqtt_client
<span class="lineNum">       3 </span>            :  * Author : S. Hamblett &lt;steve.hamblett@linux.com&gt;
<span class="lineNum">       4 </span>            :  * Date   : 31/05/2017
<span class="lineNum">       5 </span>            :  * Copyright :  S.Hamblett
<span class="lineNum">       6 </span>            :  */
<span class="lineNum">       7 </span>            : 
<span class="lineNum">       8 </span>            : part of mqtt_client;
<span class="lineNum">       9 </span>            : 
<span class="lineNum">      10 </span>            : /// Utility class to allow stream like access to a sized byte buffer.
<span class="lineNum">      11 </span>            : /// This class is in effect a cut-down implementation of the C# NET
<span class="lineNum">      12 </span>            : /// System.IO class with Mqtt client specific extensions.
<span class="lineNum">      13 </span>            : class MqttByteBuffer {
<span class="lineNum">      14 </span>            :   /// The current position within the buffer.
<span class="lineNum">      15 </span>            :   int _position = 0;
<span class="lineNum">      16 </span>            : 
<span class="lineNum">      17 </span>            :   /// A value representing the length of the stream in bytes.
<span class="lineNum">      18 </span>            :   int _length;
<span class="lineNum">      19 </span>            : 
<span class="lineNum">      20 </span>            :   /// The underlying byte buffer
<span class="lineNum">      21 </span>            :   typed.Uint8Buffer _buffer;
<span class="lineNum">      22 </span>            : 
<span class="lineNum">      23 </span><span class="lineCov">          5 :   MqttByteBuffer(this._buffer) {</span>
<span class="lineNum">      24 </span><span class="lineCov">         15 :     _length = _buffer.length;</span>
<span class="lineNum">      25 </span>            :   }
<span class="lineNum">      26 </span>            : 
<span class="lineNum">      27 </span><span class="lineCov">          4 :   MqttByteBuffer.fromList(List&lt;int&gt; data) {</span>
<span class="lineNum">      28 </span><span class="lineCov">          8 :     this._buffer = new typed.Uint8Buffer();</span>
<span class="lineNum">      29 </span><span class="lineCov">          8 :     this._buffer.addAll(data);</span>
<span class="lineNum">      30 </span><span class="lineCov">         12 :     _length = _buffer.length;</span>
<span class="lineNum">      31 </span>            :   }
<span class="lineNum">      32 </span>            : 
<span class="lineNum">      33 </span><span class="lineCov">          1 :   int get position =&gt; _position;</span>
<span class="lineNum">      34 </span>            : 
<span class="lineNum">      35 </span><span class="lineCov">          5 :   int get length =&gt; _length;</span>
<span class="lineNum">      36 </span>            : 
<span class="lineNum">      37 </span><span class="lineCov">          4 :   typed.Uint8Buffer get buffer =&gt; _buffer;</span>
<span class="lineNum">      38 </span>            : 
<span class="lineNum">      39 </span><span class="lineCov">          1 :   set buffer(typed.Uint8Buffer buff) =&gt; _buffer = buff;</span>
<span class="lineNum">      40 </span>            : 
<span class="lineNum">      41 </span>            :   /// Resets the position to 0
<span class="lineNum">      42 </span>            :   void reset() {
<span class="lineNum">      43 </span><span class="lineCov">          1 :     _position = 0;</span>
<span class="lineNum">      44 </span>            :   }
<span class="lineNum">      45 </span>            : 
<span class="lineNum">      46 </span>            :   // Reads a byte from the buffer and advances the position within the buffer by one
<span class="lineNum">      47 </span>            :   // byte, or returns -1 if at the end of the buffer.
<span class="lineNum">      48 </span>            :   int readByte() {
<span class="lineNum">      49 </span><span class="lineCov">         15 :     final int tmp = _buffer[_position];</span>
<span class="lineNum">      50 </span><span class="lineCov">         20 :     if (_position &lt;= (_length - 1)) {</span>
<span class="lineNum">      51 </span><span class="lineCov">         10 :       _position++;</span>
<span class="lineNum">      52 </span>            :     } else {
<span class="lineNum">      53 </span>            :       return -1;
<span class="lineNum">      54 </span>            :     }
<span class="lineNum">      55 </span>            :     return tmp;
<span class="lineNum">      56 </span>            :   }
<span class="lineNum">      57 </span>            : 
<span class="lineNum">      58 </span>            :   /// Read a short int(16 bits)
<span class="lineNum">      59 </span>            :   int readShort() {
<span class="lineNum">      60 </span><span class="lineCov">          5 :     final int high = readByte();</span>
<span class="lineNum">      61 </span><span class="lineCov">          5 :     final int low = readByte();</span>
<span class="lineNum">      62 </span><span class="lineCov">         10 :     return (high &lt;&lt; 8) + low;</span>
<span class="lineNum">      63 </span>            :   }
<span class="lineNum">      64 </span>            : 
<span class="lineNum">      65 </span>            :   /// Reads a sequence of bytes from the current
<span class="lineNum">      66 </span>            :   /// buffer and advances the position within the buffer by the number of bytes read.
<span class="lineNum">      67 </span>            :   typed.Uint8Buffer read(int count) {
<span class="lineNum">      68 </span><span class="lineCov">         30 :     if ((_length &lt; count) || (_position + count) &gt; _length) {</span>
<span class="lineNum">      69 </span><span class="lineCov">          1 :       throw new Exception(</span>
<span class="lineNum">      70 </span>            :           &quot;mqtt_client::ByteBuffer: The buffer did not have enough bytes for the read operation &quot;
<span class="lineNum">      71 </span><span class="lineCov">          3 :               &quot;length $_length, count $count, position $_position&quot;);</span>
<span class="lineNum">      72 </span>            :     }
<span class="lineNum">      73 </span><span class="lineCov">          5 :     final typed.Uint8Buffer tmp = new typed.Uint8Buffer();</span>
<span class="lineNum">      74 </span><span class="lineCov">         30 :     tmp.addAll(_buffer.getRange(_position, _position + count));</span>
<span class="lineNum">      75 </span><span class="lineCov">         10 :     _position += count;</span>
<span class="lineNum">      76 </span><span class="lineCov">          5 :     final typed.Uint8Buffer tmp2 = new typed.Uint8Buffer();</span>
<span class="lineNum">      77 </span><span class="lineCov">          5 :     tmp2.addAll(tmp);</span>
<span class="lineNum">      78 </span>            :     return tmp2;
<span class="lineNum">      79 </span>            :   }
<span class="lineNum">      80 </span>            : 
<span class="lineNum">      81 </span>            :   /// Writes a byte to the current position in the buffer and advances the position
<span class="lineNum">      82 </span>            :   //  within the buffer by one byte.
<span class="lineNum">      83 </span>            :   void writeByte(int byte) {
<span class="lineNum">      84 </span><span class="lineCov">         15 :     if (_length == _position) {</span>
<span class="lineNum">      85 </span><span class="lineCov">          8 :       _buffer.add(byte);</span>
<span class="lineNum">      86 </span><span class="lineCov">          8 :       _length++;</span>
<span class="lineNum">      87 </span>            :     } else {
<span class="lineNum">      88 </span><span class="lineCov">          6 :       _buffer[_position] = byte;</span>
<span class="lineNum">      89 </span>            :     }
<span class="lineNum">      90 </span><span class="lineCov">         10 :     _position++;</span>
<span class="lineNum">      91 </span>            :   }
<span class="lineNum">      92 </span>            : 
<span class="lineNum">      93 </span>            :   /// Write a short(16 bit)
<span class="lineNum">      94 </span>            :   void writeShort(int short) {
<span class="lineNum">      95 </span><span class="lineCov">         10 :     writeByte(short &gt;&gt; 8);</span>
<span class="lineNum">      96 </span><span class="lineCov">         10 :     writeByte(short &amp; 0xFF);</span>
<span class="lineNum">      97 </span>            :   }
<span class="lineNum">      98 </span>            : 
<span class="lineNum">      99 </span>            :   /// Writes a sequence of bytes to the current
<span class="lineNum">     100 </span>            :   /// buffer and advances the position within the buffer by the number of
<span class="lineNum">     101 </span>            :   /// bytes written.
<span class="lineNum">     102 </span>            :   void write(typed.Uint8Buffer buffer) {
<span class="lineNum">     103 </span><span class="lineCov">          5 :     if (_buffer == null) {</span>
<span class="lineNum">     104 </span><span class="lineCov">          1 :       _buffer = buffer;</span>
<span class="lineNum">     105 </span>            :     } else {
<span class="lineNum">     106 </span><span class="lineCov">         10 :       _buffer.addAll(buffer);</span>
<span class="lineNum">     107 </span>            :     }
<span class="lineNum">     108 </span><span class="lineCov">         15 :     _length = _buffer.length;</span>
<span class="lineNum">     109 </span><span class="lineCov">         10 :     _position = _length;</span>
<span class="lineNum">     110 </span>            :   }
<span class="lineNum">     111 </span>            : 
<span class="lineNum">     112 </span>            :   /// Seek. Sets the position in the buffer. If overflow occurs
<span class="lineNum">     113 </span>            :   /// the position is set to the end of the buffer.
<span class="lineNum">     114 </span>            :   void seek(int seek) {
<span class="lineNum">     115 </span><span class="lineCov">         14 :     if ((seek &lt;= _length) &amp;&amp; (seek &gt;= 0)) {</span>
<span class="lineNum">     116 </span><span class="lineCov">          4 :       _position = seek;</span>
<span class="lineNum">     117 </span>            :     } else
<span class="lineNum">     118 </span><span class="lineCov">          2 :       _position = _length;</span>
<span class="lineNum">     119 </span>            :   }
<span class="lineNum">     120 </span>            : 
<span class="lineNum">     121 </span>            :   /// Writes an MQTT string member
<span class="lineNum">     122 </span>            :   void writeMqttStringM(String stringToWrite) {
<span class="lineNum">     123 </span><span class="lineCov">          4 :     writeMqttString(this, stringToWrite);</span>
<span class="lineNum">     124 </span>            :   }
<span class="lineNum">     125 </span>            : 
<span class="lineNum">     126 </span>            :   /// Writes an MQTT string.
<span class="lineNum">     127 </span>            :   /// stringStream - The stream containing the string to write.
<span class="lineNum">     128 </span>            :   /// stringToWrite - The string to write.
<span class="lineNum">     129 </span>            :   static void writeMqttString(MqttByteBuffer stringStream,
<span class="lineNum">     130 </span>            :       String stringToWrite) {
<span class="lineNum">     131 </span><span class="lineCov">          4 :     final MqttEncoding enc = new MqttEncoding();</span>
<span class="lineNum">     132 </span><span class="lineCov">          4 :     final typed.Uint8Buffer stringBytes = enc.getBytes(stringToWrite);</span>
<span class="lineNum">     133 </span><span class="lineCov">          4 :     stringStream.write(stringBytes);</span>
<span class="lineNum">     134 </span>            :   }
<span class="lineNum">     135 </span>            : 
<span class="lineNum">     136 </span>            :   /// Reads an MQTT string from the underlying stream member
<span class="lineNum">     137 </span>            :   String readMqttStringM() {
<span class="lineNum">     138 </span><span class="lineCov">          4 :     return MqttByteBuffer.readMqttString(this);</span>
<span class="lineNum">     139 </span>            :   }
<span class="lineNum">     140 </span>            : 
<span class="lineNum">     141 </span>            :   /// Reads an MQTT string from the underlying stream.
<span class="lineNum">     142 </span>            :   static String readMqttString(MqttByteBuffer buffer) {
<span class="lineNum">     143 </span>            :     // Read and check the length
<span class="lineNum">     144 </span><span class="lineCov">          4 :     final typed.Uint8Buffer lengthBytes = buffer.read(2);</span>
<span class="lineNum">     145 </span><span class="lineCov">          4 :     final MqttEncoding enc = new MqttEncoding();</span>
<span class="lineNum">     146 </span><span class="lineCov">          4 :     final int stringLength = enc.getCharCount(lengthBytes);</span>
<span class="lineNum">     147 </span><span class="lineCov">          4 :     final typed.Uint8Buffer stringBuff = buffer.read(stringLength);</span>
<span class="lineNum">     148 </span><span class="lineCov">          4 :     return enc.getString(stringBuff);</span>
<span class="lineNum">     149 </span>            :   }
<span class="lineNum">     150 </span>            : }
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.10</a></td></tr>
  </table>
  <br>

</body>
</html>
